//
//  GreenHuaCheckViewController.m
//  Qihuo
//
//  Created by Vencoo on 14-1-10.
//  Copyright (c) 2014年 Vencoo. All rights reserved.
//

#import "GreenHuaCheckViewController.h"
#import "GreenHuaCell.h"
@interface GreenHuaCheckViewController ()
{
}
@end

@implementation GreenHuaCheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.checkURL = resultOfGreenHua;
    self.checkNames = @[@"灌乔木枝叶茂盛",@"无虫害枯枝枯叶",@"不漏黄土",@"绿地内无纸屑",@"无残枝败叶",@"清洁整齐",@"备注"];
    [super viewDidLoad];
    self.title = @"大厦外围绿化巡检记录";
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.floorNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    int row = 0;
    GreenHuaCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[GreenHuaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        if (self.floorCheckDatas.count > 0)
        {
            NSArray *checkDatas = [self.floorCheckDatas objectAtIndex:indexPath.row];
            for (int i = 0; i < checkDatas.count; i ++)
            {
                NSDictionary *checkDic = [checkDatas objectAtIndex:i];
                NSString *checkResult = @"";
                if ([checkDic objectForKey:@"checkRemark"])
                {
                    checkResult = [NSString stringWithFormat:@"%@",[checkDic objectForKey:@"checkRemark"]];
                }
//                else if ([checkDic objectForKey:@"isNomal"])
//                {
//                    checkResult = [NSString stringWithFormat:@"%@",[checkDic objectForKey:@"isNomal"]];
//                }
                CGSize mySz;
                UIControl *control;
                if ([checkResult isEqualToString:@"正常"])
                {
                    UIImage *rightImage = [UIImage imageNamed:@"正常"];
                    mySz = rightImage.size;
                    UIImageView *rightView = [[UIImageView alloc]initWithImage:rightImage];
                    control = (UIControl*)rightView;
                }
                else
                {
                    UILabel *resultLb = [UILabel new];
                    resultLb.text = checkResult;
                    resultLb.font = [UIFont systemFontOfSize:20];
                    mySz = [[NSString stringWithFormat:@"啊啊啊啊"] sizeWithFont:resultLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
                    
                    resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height);
                    control = (UIControl *)resultLb;
                    if (resultLb.text.length <= 4)
                    {
                        resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height);
                    }
                    else if(resultLb.text.length <= 8)
                    {
                        resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*2);
                        if (row < 1)
                        {
                            cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*1.5);
                            row = 1;
                        }
                    }
                    else if(resultLb.text.length <= 12)
                    {
                        resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*3);
                        if (row < 2)
                        {
                            cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*1.5);
                            row = 2;
                        }
                    }
                    else if (resultLb.text.length <= 16)
                    {
                        resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*4);
                        if (row < 3)
                        {
                            cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*2.5);
                            row = 3;
                        }
                    }
                    else if (resultLb.text.length <= 20)
                    {
                        resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*5);
                        if (row < 4)
                        {
                            cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*2.9);
                            row = 4;
                        }
                    }
                    resultLb.numberOfLines = 0;
                    resultLb.textAlignment = 1;
                }
                [cell addSubview:control];
            }
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.floorNames.count > 0) {
        cell.floorNameLb.text = [self.floorNames objectAtIndex:indexPath.row];
    }
    if (self.floorRemarks.count > 0) {
        cell.remarkLb.text = [self.floorRemarks objectAtIndex:indexPath.row];
    }
    return cell;
}
#pragma mark - request
- (void)requestDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSLog(@"查询%@",queryDic);
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];
    
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"floorName"])
    {
        self.floorNames = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"floorRemark"])
    {
        self.floorRemarks = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"checkData"])
    {
        self.floorCheckDatas = [NSMutableArray array];
    }
    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        if ([queryDic objectForKey:@"floorName"]) {
            [self.floorNames addObject:[queryDic objectForKey:@"floorName"]];
        }
        if ([queryDic objectForKey:@"floorRemark"]) {
            [self.floorRemarks addObject:[queryDic objectForKey:@"floorRemark"]];
        }
        if ([queryDic objectForKey:@"checkData"]) {
            [self.floorCheckDatas addObject:[queryDic objectForKey:@"checkData"]];
        }
    }
    [self.mainTable reloadData];
}
- (void)requestFail
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *failAlert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"请求数据失败！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [failAlert show];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
